package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.AgentCOADAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.AgentCOADataList;
import com.mbc.rmt.entities.AgentCOA;
import com.mbc.rmt.entities.AgentCOARpt;


public class AgentCOAMgr extends AbstractMgr {

	private AgentCOADAO agentcoaDAO;
	private LogDAO logDAO;

	public AgentCOAMgr() {
	}

	public AgentCOAMgr(String sessionId) {
		this.sessionId = sessionId;
	}

	public void create(AgentCOA entity) throws Exception {
		openConnection();
		try {
			agentcoaDAO = RemittanceDAOFactory.getAgentCOADAO(dbManager
					.getConnection());
			entity.setAgent_coa_id(KeyGenerator.generateKey(getConnection()));
			agentcoaDAO.create(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("AgentCOA");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getAgent_coa_id());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__SAVE_DATA);
			logDAO.getData(l_logentity);
			
		} catch (SQLException e) {
			if (DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e
					.getErrorCode())
				throw new DataDuplicationException(e.getMessage());
			else
				throw e;
		} finally {
			closeConnection();
		}
	}

	public void update(AgentCOA entity) throws Exception {
		openConnection();
		try {
			agentcoaDAO = RemittanceDAOFactory.getAgentCOADAO(dbManager
					.getConnection());
			agentcoaDAO.update(entity);
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("AgentCOA");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getAgent_coa_id());
			l_logentity.setUserid(entity.getUserid());
			l_logentity.setLogstatus(Log.__UPDATE_DATA);
			logDAO.getData(l_logentity);
			
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public void delete(long key, long userid) throws Exception {
		openConnection();
		try {
			agentcoaDAO = RemittanceDAOFactory.getAgentCOADAO(dbManager
					.getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("AgentCOA");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(key);
			l_logentity.setUserid(userid);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			agentcoaDAO.delete(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public AgentCOA find(long key) throws Exception {
		openConnection();
		try {
			agentcoaDAO = RemittanceDAOFactory.getAgentCOADAO(dbManager
					.getConnection());
			return agentcoaDAO.find(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public AgentCOA findByCode(String code,long branchId) throws Exception {
		openConnection();
		try {
			agentcoaDAO = RemittanceDAOFactory.getAgentCOADAO(dbManager
					.getConnection());
			return agentcoaDAO.findByCode(code,branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public List<AgentCOA> findAll() throws Exception {
		openConnection();
		try {
			agentcoaDAO = RemittanceDAOFactory.getAgentCOADAO(dbManager
					.getConnection());
			return agentcoaDAO.findAll();
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public AgentCOADataList findAgentCOAs(AgentCOA cri) throws Exception {
		openConnection();
		try {
			agentcoaDAO = RemittanceDAOFactory.getAgentCOADAO(dbManager
					.getConnection());
			return agentcoaDAO.findAgentCOAs(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public AgentCOADataList getAgentCOAList(AgentCOARpt info)
			throws Exception {
		openConnection();
		try {
			agentcoaDAO = RemittanceDAOFactory.getAgentCOADAO(dbManager
					.getConnection());
			return agentcoaDAO.getAgentCOAList(info);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}

	public JasperPrint prepareAgentCOAReport(AgentCOARpt info) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			agentcoaDAO = RemittanceDAOFactory.getAgentCOADAO(dbManager
					.getConnection());

			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(
					agentcoaDAO.prepareAgentCOAReport(info));

			JasperReport l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH
							+ "reports/AgentCOARpt.jasper");
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("company", "MBC");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
}
